# coding = utf-8
# pip install -U crawles  更新命令
import queue
from queue import Queue
from threading import Thread
import requests

from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(7)


q = Queue(22)


def image_get(data):
    response = requests.get(url, headers=headers, params=data, cookies=cookies)
    for i in response.json()['data']['object_list']:
        image_url = i['photo']['path']
        image_name = 'image/' + image_url.split('/')[-1]
        q.put([image_url, image_name])


def image_save():
    while True:
        try:
            image_url, image_name = q.get(timeout=5)
        except queue.Empty:
            break

        f = open(image_name, 'wb')
        f.write(requests.get(image_url).content)
        f.close()

        print(image_name)


for i in range(10):
    Thread(target=image_save).start()

for i in range(0, 24 * 150, 24):  # 7
    url = "https://www.duitang.com/napi/blog/list/by_search/"
    cookies = {
        "sessionid": "2519d0d8-03d5-4be0-9266-3270915b8776",
        "_fromcat": "category",
        "Hm_lvt_d8276dcc8bdfef6bb9d5bc9e3bcfcaf4": "1676038878,1676111012,1676268339,1676288591",
        "Hm_lpvt_d8276dcc8bdfef6bb9d5bc9e3bcfcaf4": "1676289436",
    }
    headers = {
        "authority": "www.duitang.com",
        "accept": "text/plain, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "referer": "https://www.duitang.com/category/?cat=wallpaper",
        "sec-ch-ua": "\\",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\\",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
    }

    data = {
        "include_fields": "like_count,sender,album,msg,reply_count,top_comments",
        "kw": "壁纸",
        "start": f"{i}",
        "_": "1676288618706",
    }
    pool.submit(image_get, data)
    # Thread(target=image_get, args=(data,)).start()

# https://c-ssl.dtstatic.com/uploads/blog/202205/25/20220525144600_ec2c5.thumb.400_0.jpeg_webp
# https://c-ssl.duitang.com/uploads/blog/202205/16/20220516164359_e2a6f.jpg
# https://c-ssl.dtstatic.com/uploads/blog/202205/16/20220516164359_e2a6f.jpg

# 站长素材（https://sc.chinaz.com/tupian/） 使用线程池获取

